#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar led[ ]={0xC0,0xF9,0xa4,0xB0,0x99,0x92,0x82,0xF8,0x80,
0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
uchar k=8,num=8;
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
void delay(uchar x){
    uchar i,j;
    for(i=0;i<x;i++)
	  for(j=0;j<120;j++);
}
uchar scankey()
{ P1=0X0F;
	if(P1!=0X0F){
		delay(10); 
		if(P1!=0x0f)
			{
				P1=0xef;
				if(key1==0)num=0;
				if(key2==0)num=1;
				if(key3==0)num=2;

			}
			{
				P1=0xdf;
				if(key1==0)num=3;
				if(key2==0)num=4;
				if(key3==0)num=5;

			}
			{
				P1=0xbf;
			if(key1==0)num=6;
			if(key2==0)num=7;
			if(key3==0)num=8;

			}
}
	return num;
}
void main(){
	while(1){
	switch(scankey()){
		case  0:k=0;break;
		case  1:k=1;break;
    case  2:k=2;break;
		case  3:k=3;break;
		case  4:k=4;break;
		case  5:k=5;break;
		case  6:k=6;break;
		case  7:k=7;break;
		case  8:k=8;break;
		default:break;
	}
	P0=~led[k];}
}